package com.xxxy.controller;

import com.xxxy.client.QuartzClient;
import com.xxxy.commonResult.CommonResult;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.log.Log;
import com.xxxy.model.ExaminationPaper;
import com.xxxy.model.Question;
import com.xxxy.response.ExaminationPaperResponse;
import com.xxxy.response.QuestionListResponse;
import com.xxxy.service.ExaminationPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sjh
 * @date 2022/2/27 15:59
 */
@RestController
@Api(tags = "试卷管理")
@RequestMapping("/examinationPaper")
public class ExaminationPaperController {

    @Autowired
    ExaminationPaperService examinationPaperService;

    @Autowired
    QuartzClient quartzClient;

    @Log(module = "试卷管理", description = "获取试卷列表")
    @PostMapping(value = "/list")
    @ApiOperation(value = "试卷列表")
    public CommonResult<CommonResultPage> listQuestion(@ApiParam("页数") @RequestParam(defaultValue = "1") Integer page,
                                                       @ApiParam("一页条数") @RequestParam(defaultValue = "10") Integer size,
                                                       @ApiParam("学院id") @RequestParam(required = false) Integer facultyId,
                                                       @ApiParam("课程id") @RequestParam(required = false) Integer courseId,
                                                       @ApiParam("试卷名") @RequestParam(required = false) String examinationName,
                                                       @ApiParam("批阅（是：1，否：0）") @RequestParam(required = false) Integer isReviewed,
                                                       @ApiParam("教师id") @RequestParam(required = false) String teacherAccount) {
        return CommonResult.success(examinationPaperService.listExamination(page, size, facultyId, courseId, examinationName, isReviewed, teacherAccount));
    }

    @Log(module = "试卷管理", description = "新增单张试卷")
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增单张试卷")
    public CommonResult<Boolean> addExaminationPaper(@ApiParam("试卷信息") @RequestBody ExaminationPaper examinationPaper) {
        if (examinationPaperService.save(examinationPaper)) {
            quartzClient.addQuartz(examinationPaper.getId());
        }
        return CommonResult.failed();
    }

    @Log(module = "试卷管理", description = "删除单个试卷")
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除单个试卷")
    public CommonResult<Boolean> deleteExaminationPaper(@ApiParam("试卷id") @RequestParam Integer examinationPaperId) {
        if (examinationPaperService.removeById(examinationPaperId)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "试卷管理", description = "更新单个试卷信息")
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新单个试卷信息")
    public CommonResult<Boolean> updateExaminationPaper(@ApiParam("试题") @RequestBody ExaminationPaper examinationPaper) {
        if (examinationPaperService.updateById(examinationPaper)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Log(module = "试卷管理", description = "获取单张试卷信息")
    @GetMapping(value = "/info/{id}")
    @ApiOperation(value = "单张试卷信息")
    public CommonResult<ExaminationPaperResponse> getExaminationPaper(@ApiParam("试卷id") @PathVariable Integer id) {
        return CommonResult.success(examinationPaperService.getById(id));
    }

    @Log(module = "试卷管理", description = "获取试卷的试题信息")
    @GetMapping(value = "/info/question/{id}")
    @ApiOperation(value = "试卷的试题信息")
    public CommonResult<List<QuestionListResponse>> getExaminationPaperQuestion(@ApiParam("试卷id") @PathVariable Integer id) {
        return CommonResult.success(examinationPaperService.getQuestionByExaminationPaperId(id));
    }


}
